import Request from 'utils/request';

import RecommendGoodsForm from 'models/goods/recommendGoodsForm';

export default class RecommendGoodsApi {
  static getRecommendGoodsList(search, pagination) {
    const apiName = 'api/recommendgoods/list';

    let postData = search.getObject(pagination);

    return Request.post(apiName, postData).then(res => {
      let goodsList = res.data;
      goodsList = _.map(goodsList, goods => {
        goods = new RecommendGoodsForm(goods);
        return goods;
      });

      pagination.totalCount = res.rows;
      return goodsList;
    });
  }

  static getGoodsDropdown(goodsForm, type) {
    let branchId = goodsForm.branchId;
    if (branchId === '') {
      return Promise.resolve([]);
    }

    const apiName = 'api/recommendgoods/getNoActivityGoods';
    let postData = {};

    if (type === 'partner') {
      let partnerId = goodsForm.partnerId;
      if (partnerId !== '') {
        postData.partnerId = partnerId;
      }
    } else {
      postData.branchId = branchId;
    }
    return Request.post(apiName, postData).then(res => {
      let goodsList = res.data;
      goodsList = _.map(goodsList, goods => {
        return {
          id: goods.goodsDefId,
          name: goods.goodsName
        };
      });
      return goodsList;
    });
  }

  static addRecommendGoods(goodsForm) {
    const apiName = 'api/recommendgoods/save';
    let postData = goodsForm.getObject();

    return Request.post(apiName, postData).then(res => {
      return res.msg;
    });
  }

  static updateRecommendGoods(goodsForm) {
    const apiName = 'api/recommendgoods/update';
    let postData = goodsForm.getObject();

    return Request.post(apiName, postData).then(res => {
      return res.msg;
    });
  }
}
